package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.food.mapper.GoodsMainFoodMapper;
import com.dycx.food.dto.GoodsDTO;
import com.dycx.food.dto.GoodsQueryDTO;
import com.dycx.food.entity.GoodsMainDO;
import com.dycx.food.service.GoodsMainFoodService;
import com.dycx.food.vo.GoodsVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GoodsMainFoodImpl extends ServiceImpl<GoodsMainFoodMapper, GoodsMainDO> implements GoodsMainFoodService {

    private GoodsMainFoodMapper goodsMainMapper;

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<GoodsVO> getPage(GoodsQueryDTO goodsDto) {
        Page<GoodsVO> page = new Page<>(goodsDto.getPageIndex(), goodsDto.getPageSize());  // 查询第1页，每页返回5条
        page = goodsMainMapper.getPage(page, goodsDto);
        page.getRecords().forEach(item->{
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });
        return page;
    }

    /**
     * 查询系列下的所有公共商品
     *
     * @param goodsDTO
     * @return
     */
    @Override
    public List<GoodsVO> getMainGoodsListByDesign(GoodsDTO goodsDTO) {
        LambdaQueryWrapper<GoodsMainDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsMainDO::getIsShelf, true);
        wrapper.orderByDesc(GoodsMainDO::getSort);

        if (goodsDTO.getGoodsClassId() != null) {
            wrapper.eq(GoodsMainDO::getGoodsClassId, goodsDTO.getGoodsClassId());
        }

        if (goodsDTO.getBrandId() != null) {
            wrapper.eq(GoodsMainDO::getBrandId, goodsDTO.getBrandId());
        }

        if (goodsDTO.getDesignId() != null) {
            wrapper.eq(GoodsMainDO::getDesignId, goodsDTO.getDesignId());
        }

        List<GoodsVO> voList = this.list(wrapper).stream().map(item ->
                BeanUtil.toBean(item, GoodsVO.class)
        ).collect(Collectors.toList());

        voList.forEach(item->{
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });

        return voList;
    }

    /**
     * 更新排序
     *
     * @param goodsDTO
     * @return
     */
    @Override
    public boolean updateSort(GoodsDTO goodsDTO) {
        GoodsMainDO goodsMainDO = new GoodsMainDO();
        goodsMainDO.setId(goodsDTO.getId());
        goodsMainDO.setSort(goodsDTO.getSort());

        return this.updateById(goodsMainDO);
    }

    /**
     * 获取排序最大值
     *
     * @return
     */
    @Override
    public int getMaxSort() {
        return goodsMainMapper.getMaxSort();
    }
}
